
define(function () {
    function UserList(options) {
        this.init(options);

    }

    UserList.prototype = {
        init: function (options) {
            this.options = options;
        },
        setOptions: function (options) {
            this.options = $.extend(this.options, options);
        },
        show: function (data) {
            var that = this;
            that.setOptions(data);
            var url = this.options.host + "/commonChoose/page/userList";
            var params = {id: this.options.id};
            $("#modal_user_con_body").empty();
            $("#modal_user_con_body").load(url, params, function () {
                that.loadContentFinish();
            });
        },
        loadContentFinish: function () {
            var that = this;
            that.initModalEvents();
            that.initModalTable();
            that.showModal();
        },

        initModalEvents: function () {
            var that = this;
            // 查询组件
            $("#search_tagId").select2({width: "200", allowClear: true});
            // 搜索
            $("#btn_user_search").off("click.btn_user_search").on("click.btn_user_search", function () {
                that.initModalTable();
            });
            // 删除
            $("#table_album_user tbody").off("click.op_user_recommend_off").on("click.op_user_recommend_off", "a[data-sign=op_user_recommend_off]", function () {
                var $op_user = $(this).closest("span[data-sign=op_user]");
                that.opRecommend({
                    id: $op_user.data("id"),
                    op: "0"
                });
            });
            // 添加
            $("#table_album_user tbody").off("click.op_user_recommend_on").on("click.op_user_recommend_on", "a[data-sign=op_user_recommend_on]", function () {
                var $op_user = $(this).closest("span[data-sign=op_user]");
                that.opRecommend({
                    id: $op_user.data("id"),
                    op: "1"
                });
            });

            // 在线播放
            $("#table_album_user tbody").off("click.op_user_view").on("click.op_user_view", "a[data-sign=op_user_view]", function () {
                var $op_user = $(this).closest("span[data-sign=op_user]");
                that.opView({
                    id: $op_user.data("id"),
                    $a: $(this)
                });
            });

            // 关闭
            $("#btn_album_close").off("click.btn_album_close").on("click.btn_album_close", function () {
                that.options.callback_close();
            });

        },

        initModalTable: function () {
            var that = this;
            this.modalDataTable = $("#table_album_user").dataTable({
                iDisplayStart:0,
                iDisplayLength: 20,
                bProcessing: true,
                bServerSide: true,
                bSort: false,
                bFilter: false,
                bAutoWidth: false,
                bDestroy: true,
                sDom: "tr<'row-fluid'<'span6'i><'span6'p>>",
                sAjaxSource: this.options.host + "/commonChoose/userlist.json",
                sServerMethod: "POST",
                aoColumns: [
                    { sTitle: "ID", mData: "userId"},
                    { sTitle: "头像", mData: null,
                        fnRender: function (obj) {
                            var picture = obj.aData.picture;
                            if (picture == undefined || "" == picture) {
                                return "<a id='a_content_pic' href=''></a>"
                            }
                            var pictureList = picture.split(",");
                            var len = pictureList.length <= 0 ? 0 : pictureList.length - 1;
                            return "<a id='a_content_pic' href='" + pictureList[len]
                                + "' target='_blank'><img style='width: 30px; height: 30px;' id='img_content_pic' src='"
                                + pictureList[len] + "' alt=''/></a>"
                        }
                    },
                    { sTitle: "昵称", sWidth: "20%", mData: "nickName"},
                    { sTitle: "身份", mData: null,
                        fnRender: function (obj) {
                            switch (obj.aData.role) {
                                case 0:
                                    return '普通';
                                case 1:
                                    return '普通认证';
                                case 3:
                                    return '企业认证';
                                case 4:
                                    return '主播';
                                default:
                                    return '';
                            }
                        }
                    },
                    { sTitle: "等级", mData: null,
                        fnRender: function (obj) {
                            if(obj.aData.level==null){
                                return '0';
                            }else{
                                return obj.aData.level;
                            }
                        }
                    },
                    { sTitle: "系统", mData: null,
                        fnRender: function (obj) {
                            switch (obj.aData.platformId) {
                                case 1:
                                    return 'IOS';
                                case 2:
                                    return '安卓';
                                default:
                                    return '未知';
                            }
                        }
                    },
                    { sTitle: "绑定第三方", mData: null,
                        fnRender: function (obj) {
                            switch (obj.aData.userType) {
                                case 1:
                                    return 'facebook';
                                case 2:
                                    return 'twitter';
                                case 4:
                                    return '微博';
                                case 5:
                                    return 'QQ';
                                case 6:
                                    return '微信';
                                default:
                                    return '未知';
                            }
                        }
                    },
                    { sTitle: "最近登录", mData: "lastLoginTimeStr"},
                    { sTitle: "直播经历", mData: "broadCastNum"},
                    {
                        sTitle: "操作", sWidth: "20%", mData: null,
                        fnRender: function (obj) {
                            var color_recommend = "red";
                            var action_recommend = "op_user_recommend_off";
                            var recommend_on_off = "删除";
                            if (obj.aData.status != null && obj.aData.status == 0) {
                                color_recommend = "yellow";
                                action_recommend = "op_user_recommend_on";
                                recommend_on_off = "添加";
                            }
                            var ops = [
                                {color: color_recommend, sign: action_recommend, id: "", name: "", btnName: recommend_on_off},
                            ];
                            return _.template($("#temp_op").html(), {
                                sign: "op_user",
                                id: obj.aData.userId,
                                ops: ops
                            });

                        }
                    }
                ],
                fnServerParams: function (aoData) {
                    aoData = $.merge(aoData, that.getModalQueryParams());
                }
            });
        },
        getModalQueryParams: function () {
            var search_userName = $("#form_user_search input[name=search_userName]").val();
            return [
                {name: "search_userName", value: search_userName},
                {name: "search_userStatus", value: 1},
                {name: "search_id", value: this.options._id},
                {name: "search_type", value: this.options.type},
            ];
        },
        refreshModalTable: function () {
            if (this.modalDataTable) {
                this.modalDataTable.fnDraw();
            } else {
                this.initModalTable();
            }
        },
        showModal: function () {
            var that = this;
            $("#modal_album_con").modal({
                width: 800,
                maxHeight: 500
            });
            //增加关闭回调函数
            $('#modal_album_con').on('hide.bs.modal', function () {
                that.options.callback_refresh();
            })
        },
        hideModal: function () {
            $("#modal_album_con").modal("hide");
        },
        opRecommend: function (data) {
            var that = this;
            $.ajax({
                url: that.options.host + "/commonChoose/userRecommend",
                type: "post",
                data: {type:this.options.type, id:this.options._id, userId: data.id, op: data.op},
                success: function (data) {
                    if (data.rsCode == "A00000") {
                        $.gritter.add({title: "提示信息：", text: "操作成功！", time: 1000});
                        that.refreshModalTable();
                    } else {
                        $.gritter.add({title: "提示信息：", text: data.rsMsg, time: 2000});
                    }
                }
            });
        }
    }
    return UserList;
});
